//----------------------派代编辑器相关-----------------
$(document).ready(function(){
	//编辑器初始化
	var frame = window.frames['pdeditor'],
		doc = window.frames['pdeditor'].document,
		pdbd = window.frames['pdeditor'].document.body,
		browser = !!window.ActiveXObject ? 'ie':'other';
	if(browser == 'ie')	{
		pdbd.contentEditable = true;		
		pdbd.attachEvent('onpaste',function() {setTimeout(pasteFliter,1)});
		pdbd.attachEvent('onkeyup',countNum);
		pdbd.attachEvent('onclick',checkStatus);
	}else{
		doc.designMode = 'on';	
		pdbd.addEventListener('paste',function() {setTimeout(pasteFliter,1)},false);
		frame.addEventListener('keyup',countNum,false);
		frame.addEventListener('click',checkStatus,false);
	}	
	//添加iframe内部样式
		pdbd.style.cssText = 'width:630px;background-color:#ffffff;padding:0;margin:0;font-size:14px;line-height:1.8;word-break:break-all;border-radius:4px;padding:12px 0 22px 20px;';
		//text-indent:2em;
		var i_style = doc.createElement('link');
		i_style.rel = 'stylesheet';
		i_style.href = 'styles/iframe.css?v=030502';
		doc.getElementsByTagName('head')[0].appendChild(i_style);
	//swf初始化
	swfu = new SWFUpload({	//全局变量
		upload_url: "uploadimg.php",
//	    post_params : {"phpSESSID" : "<?php =PHPSESSID ?>"},
		file_size_limit : "100 MB",
		file_types : "*.jpg;*.gif;*.png;*.bmp",
		file_post_name : "filedata",
		// Event Handler Settings 
		file_queue_error_handler : fileQueueError,
		file_dialog_complete_handler : fileDialogComplete,
		upload_progress_handler : uploadProgress,
		upload_error_handler : uploadError,
		upload_success_handler : uploadSuccess,
		upload_complete_handler : uploadComplete,	
		// Button settings
		button_image_url : "images/swfupload_btn.gif?v=0320",
		button_placeholder_id : "swfuploadbtn",
		button_width: 48,
		button_height: 38,
		// Flash Settings
		flash_url : "swfupload/swfupload.swf",
		debug: false
	});	
	//标题输入框
	$('#pdtitle').focus(function(){
		if($(this).val() == '请输入帖子标题...'){
			$(this).val('').css({'color':'#323232','padding':'15px 16px 15px 16px','background-position':'-30px -437px'});
		}
	})
	$('#pdtitle').blur(function(){
		if($(this).val() == ''){
			$(this).val('请输入帖子标题...').css({'color':'#a0a0a0','padding':'15px 0 15px 32px','background-position':'17px -437px'});
		}
	})
//鼠标感应-----	
	//点击主要板块
	$('.main_section a').click(function(){
		$('.main_section a').removeClass('selected_s');
		$('#other_btn').removeClass('selected_s').addClass('other_btn').text('');
		$(this).addClass('selected_s');
	})
	//点击其他版块
	$('#other_btn').click(function(){
		$('#other_list').slideToggle('slow');		
	})
	//点击其他板块栏目
	$('#other_list ul li').click(function(){
		$('.main_section a').removeClass('selected_s');
		var secVal = $(this).text();
		$('#other_btn').removeClass('other_btn').addClass('selected_s').text(secVal);
		$('#other_list').slideUp('slow');				
	})
	//指向其他板块栏目
	$('#other_list ul li').hover(function(){
		$(this).addClass('li_hover');	
	},function(){
		$(this).removeClass('li_hover');
	})	
	//字体加粗
	$('#pdbold').click(function() {
		frame.focus();
		doc.execCommand('bold',false,null);
		checkStatus();
		frame.focus();	
	})
	//检查加粗状态
	function checkStatus() {
		var bStatus = doc.queryCommandState('bold') ? '0 -240px':'0 -200px';
		$('#pdbold').css('background-position',bStatus);
	}
	//插入图片
	insertImg = function(src) {		//全局变量，供外部uploadSuccess函数调用
		frame.focus();
		doc.execCommand('insertimage',false,src);
		frame.focus();
	}
	//字数统计
	function countNum()	{
		document.getElementById('count_num').innerHTML = browser == 'ie'? pdbd.innerText.length:pdbd.textContent.length;
	}
	//粘贴过滤
	function pasteFliter(){
		pdbd.innerHTML = filter(pdbd.innerHTML);
		countNum();
	}
	function filter(content) {
		return content.replace(/<\s*\/?\s*span[^>]*>/gi,'').replace
		(/\s*style\s*=\s*"[^"]*"/gi,'').replace(/<\s*\/?\s*pre[^>]*>/gi,'').replace(/<\s*h\d[^>]*>/gi,'<p>').replace
		(/<\s*\/?\s*h\d[^>]*>/gi,'<\p>').replace(/<\s*\/?\s*table[^>]*>/gi,'').replace(/<\s*\/?\s*tr[^>]*>/gi,'').replace(/<\s*\/?\s*td[^>]*>/gi,'').replace
		(/\s*class\s*=\s*"[^"]*"/gi,'').replace(/<\s*div[^>]*>/gi,'<p>').replace(/<\s*\/?\s*div[^>]*>/gi,'<\p>').replace(/<\s*\/?\s*code[^>]*>/gi,'').replace(/<\s*\/?\s*a\s*[^>]*>/gi,'').replace
		(/_extended\s*=\s*"[^"]*"/gi,'').replace(/<\s*\/?\s*font[^>]*>/gi,'').replace(/<\s*script[^>]*>.*<\s*\/\s*script[^>]*>/gi,'').replace
		(/<\s*\/?\s*form[^>]*>/gi,'').replace(/<\s*\/?\s*input[^>]*>/gi,'').replace(/<\s*\/?\s*textarea[^>]*>/gi,'').replace(/<!--[^-->]*-->/gi,'').replace(/<\s*\/?\s*ul[^>]*>/gi,'').replace(/<\s*\/?\s*li[^>]*>/gi,'');	//.replace(/<\s*\w+[^>]*>\s*<\s*\/?\s*\w+[^>]*>/gi,'') 空标签？
		
	};
	
	//插入视频
	$('#insertvideo').click(function(){
		$('#videopanel').slideToggle('slow');
		$('#videoaddr').focus();
	})
	//点击确定按钮	
	$('#addvideo').click(function() {
		var vURL = $('#videoaddr').val();		
		if(/swf/gi.test(vURL)){		
			vURL = /embed/gi.test(vURL) ? vURL:('<embed src="' + vURL + '" quality="high" width="480" height="400"allowScriptAccess="always" allowFullScreen="false" mode="transparent" type="application/x-shockwave-flash"></embed>')
			frame.focus();
			if(browser == 'ie'){
				doc.selection.createRange().pasteHTML(vURL);
				$('#videoaddr').val('');
				$('#videopanel').slideUp('slow');						
			}else{			
				doc.execCommand('insertHTML',false,vURL);
				$('#videoaddr').val('');
				$('#videopanel').slideUp('slow'); 
			}
		}else{
			alert('视频URL不正确，请重新输入！');
			$('#videoaddr').val('');
			return;
		}	
	})
	//点击关闭按钮
	$('#cancelvideo').click(function (){
		$('#videoaddr').val('');
		$('#videopanel').slideUp('slow'); 	
	})	
	//本地存储
	if(!!window.localStorage){
		pdbd.innerHTML = localStorage.getItem('pdsaved') || '';
		countNum();				
		setInterval(function(){
			var pddata = pdbd.innerHTML || '';
				localStorage.setItem('pdsaved',pddata);
		},5000);
	}else{
		var udObj = document.getElementById('pd_userdata');		
		udObj.load('pdsavedfile');      
		pdbd.innerHTML = udObj.getAttribute('pdsaved') || '';
		countNum();	
		setInterval(function(){
			var pddata = pdbd.innerHTML || '';
				udObj.setAttribute('pdsaved',pddata);
				udObj.save('pdsavedfile');	
		},5000);	
	}
		
//提交文章
	$('#pdsubmit').click(function(){
		var pdtitle = $.trim($('#pdtitle').val()).replace(/</gi,'').replace(/>/gi,'').replace(/script/gi,'');
		if(pdtitle == '' || pdtitle == '请输入帖子标题...') {
			alert('请填写标题!');
			return;
		}
		var	pdsection = $('.selected_s').text(),
			pdcontent = filter(pdbd.innerHTML);
			//检查内容为空
		if(pdcontent == '' || pdcontent == '<br>') {
			alert('请填写内容!');
			return;
		}
			
		$.post('submit_do.php',{title:pdtitle,section:pdsection,content:pdcontent},function(data,textstatus){
			//判断提交状态
			if(textstatus == 'success'){			
				if(!!window.localStorage){
					localStorage.setItem('pdsaved','');
				}else{
					document.getElementById('pd_userdata').setAttribute('pdsaved','');
					document.getElementById('pd_userdata').save('pdsavedfile');
				}
				location.href = "article_detail.php?aid=" + data;
			}
		});		
	});

})

//-----------------------------------------------------------------------------------------------------
//-------------------------------------swfupload 相关函数-------------------------------------------------
function fileDialogComplete(numFilesSelected, numFilesQueued) { //选取完图片后开始上传
	if (numFilesQueued > 0) {
		this.startUpload();
		document.getElementById("upload_bg").style.display = "inline-block";
	}
}
function uploadSuccess(file, serverData) { //上传成功后插入图片
	insertImg(serverData);
	var a = document.getElementById("upload_color"),
		b = document.getElementById("upload_percent_num");
	a.style.width = "200px"; 
	b.innerHTML = 100;
	setTimeout(function(){
		a.style.width = "0px"; 
		b.innerHTML = 0;
		$('#upload_bg').fadeOut('slow'); //document.getElementById("upload_bg").style.display = "none";
	},3000)
}
function uploadComplete(file) {		//一张图片上传完成
	if (this.getStats().files_queued > 0) {
		this.startUpload();
		document.getElementById("upload_bg").style.display = "inline-block";
	}
}
function fileQueueError(file, errorCode, message) {	//加入上传队列出错
	switch (errorCode) {
	case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
		alert('图片大小为零！');
		break;
	case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
		alert('图片大小限制为2M！');
		break;
	default:
		alert(message);
		break;
	}
}
function uploadProgress(file, bytesLoaded) {	//显示上传进度,进度条总宽度为170px
	var percent = Math.ceil((bytesLoaded / file.size) * 100);
	document.getElementById("upload_color").style.width = percent / 100 * 170 + "px"; 
	document.getElementById("upload_percent_num").innerHTML = percent;
}
function uploadError(file, errorCode, message) {  //上传过程出错
	if(errorCode == SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED){
 		alert('上传已终止，请重试！');
	} else {
		alert(message);
	}
}






